home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume3 / getpath < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  15.2 KB

  1. Path: xanth!mcnc!rutgers!cmcl2!husc6!necntc!ncoast!allbery
  2. From: dtinker@gpu.utcs.toronto.edu ("Prof. David Tinker")
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i046: Getpath.c Submission
  5. Message-ID: <8806112232.AA00922@gpu.utcs.toronto.edu>
  6. Date: 11 Jun 88 21:12:43 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: dtinker@gpu.utcs.toronto.edu ("Prof. David Tinker")
  9. Lines: 370
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. comp.sources.misc: Volume 3, Issue 46
  13. Submitted-By: ""Prof. David Tinker"" <dtinker@gpu.utcs.toronto.edu>
  14. Archive-Name: getpath
  15.  
  16. =======================================
  17. GETPATH.C :  Part 1 of 1
  18.  
  19. Parse string containing DOS file path and return components in a struct.
  20.  
  21. In the course of development of a text-based application, I found a
  22. need to parse MS-DOS (tm) file names into [drive:][path][name][extension].
  23. Since a utility to do so was not readily available, I took a couple of hours
  24. out to write one.  It occured to me that others might find such a utility
  25. useful as well, so here it is:  I have chosen to make it "copylefted" rather
  26. than public domain, but no charge or donation is requested.
  27.  
  28. -----------------[ Cut Here ]--------------------
  29. #! /bin/sh
  30. # This is a shell archive.  Remove anything before this line, then unpack
  31. # it by saving it into a file and typing "sh file".  To overwrite existing
  32. # files, type "sh file -c".  You can also feed this as standard input via
  33. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  34. # will see the following message at the end:
  35. #        "End of shell archive."
  36. # Contents:  getpath.doc getpath.c getpath.h sample.c
  37. # Wrapped by dtinker@utgpu.UUCP on Thu May 19 17:06:15 1988
  38. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  39. if test -f getpath.doc -a "${1}" != "-c" ; then 
  40.   echo shar: Will not over-write existing file \"getpath.doc\"
  41. else
  42. echo shar: Extracting \"getpath.doc\" \(479 characters\)
  43. sed "s/^X//" >getpath.doc <<'END_OF_getpath.doc'
  44. X/*
  45. X * getpath.doc
  46. X */
  47. X
  48. X/*
  49. X This little utility function parses a DOS file name and returns a structure
  50. X containing the drive, path, name and extension of the file.  If the drive 
  51. X and path are omitted in the input filename, getpath supplies the current
  52. X working directory by default.  The function was written for the Aztec C (tm)
  53. X compiler, but should be compatible with any standard C compiler.  A small
  54. X program, sample.c is included to show the operation of the function.
  55. X*/
  56. END_OF_getpath.doc
  57. if test 479 -ne `wc -c <getpath.doc`; then
  58.     echo shar: \"getpath.doc\" unpacked with wrong size!
  59. fi
  60. # end of overwriting check
  61. fi
  62. if test -f getpath.c -a "${1}" != "-c" ; then 
  63.   echo shar: Will not over-write existing file \"getpath.c\"
  64. else
  65. echo shar: Extracting \"getpath.c\" \(8092 characters\)
  66. sed "s/^X//" >getpath.c <<'END_OF_getpath.c'
  67. X
  68. X/************************************************************************
  69. X *                                                                      *
  70. X *                getpath.c     -----    get DOS path                   *
  71. X *                                                                      *
  72. X *                 Version 1.00  ---   May 19, 1988                     *
  73. X *                                                                      *
  74. X *              Copyright (c) 1988 by David O. Tinker                   *
  75. X *            All Rights Reserved except as noted below.                *
  76. X *                                                                      *
  77. X * This software may be used without charge for non-commercial purposes *
  78. X * provided this Copyright notice is not removed. Permission is granted *
  79. X * to make alterations to this software, provided credit is given to    *
  80. X * the original author in any altered version.                          *
  81. X *                                                                      *
  82. X *                      David O. Tinker                                 *
  83. X *                      Department of Biochemistry                      *
  84. X *                      University of Toronto                           *
  85. X *                      Toronto, Canada, M5S 1A8                        *
  86. X *                      E-mail:                                         *
  87. X *                      dtinker@utgpu.UUCP                              *
  88. X *                      dtinker@utoronto.BITNET                         *
  89. X *                                                                      *
  90. X ***********************************************************************/
  91. X
  92. X
  93. X#include "getpath.h"
  94. X
  95. X/************************************************
  96. X *                                              *
  97. X * path_t *getpath(filename)                    *
  98. X * char *filename;                              *
  99. X *                                              *
  100. X * Instantiate structure to hold path data      *
  101. X *                                              *
  102. X * Input: DOS File Name string,                 *
  103. X *        Format: [d:][\path]name[.ext]         *
  104. X *                                              *
  105. X * Returns: Structure of type path_t            *
  106. X *          (see getpath.h                      *
  107. X *                                              *
  108. X * Calling:                                     *
  109. X *      char *file_name;                        *
  110. X *      path_t *path, *getpath();               *
  111. X *      ...                                     *
  112. X *      path = getpath(file_name);              *
  113. X *                                              *
  114. X * Library Functions Required:                  *
  115. X *      *getcwd(2);                             *
  116. X *      (get current working directory)         *
  117. X *      *strcat(2);                             *
  118. X *                                              *
  119. X * Bugs:                                        *
  120. X *      Does not check for legal filename       *
  121. X *                                              *
  122. X *                                              *
  123. X * Author:      David O. Tinker                 *
  124. X *                                              *
  125. X ***********************************************/
  126. X
  127. Xpath_t *getpath(filename)
  128. Xchar *filename;
  129. X{
  130. X        path_t *new;                    /* structure to hold data           */
  131. X        char *file_disk,                /* disk location of active file     */
  132. X             *path,                     /* path to active file              */
  133. X             *file_name,                /* name of active file (no .ext)    */
  134. X             *file_ext,                 /* extension of active file         */
  135. X             *fil_loc,                  /* temporary cwd buffer             */
  136. X             *curr_path;                /* current working directory        */
  137. X        int i, j = 0, k, name_len, no_of_dirs = 0;
  138. X        BOOLEAN path_p = FALSE;
  139. X        char *directory[10],       /* 10 subdirectories ought be enough ! */
  140. X             buff[13];             /* buffer to hold file & directory names */
  141. X        void error();
  142. X
  143. X        name_len = strlen(filename);
  144. X
  145. X        /* get current working directory */
  146. X
  147. X        fil_loc= getcwd(NULL,64);
  148. X        curr_path = CALLOC((strlen(fil_loc) + 2), char);
  149. X        curr_path[0] = '\134';
  150. X        curr_path = strcat(curr_path, fil_loc);
  151. X        free((char *)fil_loc);
  152. X
  153. X        /* find disk and directory level of filename */
  154. X
  155. X        file_disk = CALLOC(3, char);
  156. X        for (i = 0; i < name_len; i++) {
  157. X                if((filename[i] == '\072') || (filename[i] == '\057')
  158. X                   || (filename[i] == '\134'))
  159. X                        path_p = TRUE;
  160. X                if(filename[i] == '\072') { /* set file_disk name */
  161. X                        j = i;
  162. X                        file_disk[0] = filename[--j];
  163. X                        file_disk[1] = filename[i];
  164. X                        j += 2;
  165. X                }
  166. X                if((filename[i] == '\057') || (filename[i] == '\134'))
  167. X                        ++no_of_dirs;
  168. X        }
  169. X
  170. X        if(no_of_dirs) {
  171. X                buff[0] = '\134';
  172. X                buff[1] = '\0';
  173. X                directory[0] = CALLOC(2, char);
  174. X                strcpy(directory[0], buff);
  175. X        }
  176. X
  177. X        for(i = 1, k = 0; i < no_of_dirs; i++) {
  178. X                if(i == 1)
  179. X                        ++j;
  180. X                buff[k] = filename[j];
  181. X                ++j;
  182. X                while((filename[j] != '\057') && (filename[j] != '\134')) {
  183. X                        ++k;
  184. X                        buff[k] = filename[j];
  185. X                        ++j;
  186. X                        if(filename[j] == '\0') break;
  187. X                }
  188. X                buff[++k] = '\0';
  189. X                directory[i] = CALLOC((strlen(buff) + 1), char);
  190. X                strcpy(directory[i], buff);
  191. X                k = 0;
  192. X        }
  193. X        for(i = 0, k = 1; i < no_of_dirs; i++) {
  194. X                k += strlen(directory[i]);
  195. X        }
  196. X        if(no_of_dirs) {
  197. X                ++j;
  198. X                path = CALLOC(k, char);
  199. X                strcpy(path, directory[0]);
  200. X                for(i = 1; i < no_of_dirs; i++)
  201. X                        path = strcat(path, directory[i]);
  202. X        }
  203. X        else 
  204. X                path = CALLOC(1, char);
  205. X
  206. X        /* parse remaining characters in filename for name and ext */
  207. X        for(i = 0, k = 0; i < 8; i++) {
  208. X                buff[k] = filename[j];
  209. X                ++k;
  210. X                ++j;
  211. X                if((filename[j] == '.') || (filename[j] == '\0')) {
  212. X                        if(filename[j] == '.') ++j;
  213. X                        break;
  214. X                }
  215. X        }
  216. X        buff[k] = '\0';
  217. X        file_name = CALLOC((strlen(buff) + 1), char);
  218. X        strcpy(file_name, buff);
  219. X        if(filename[j] != '\0'){
  220. X                for(i = 0, k = 0; i < 3; i++) {
  221. X                        buff[k] = filename[j];
  222. X                        ++k;
  223. X                        ++j;
  224. X                        if(filename[j] == '\0') break;
  225. X                }
  226. X                buff[k] = '\0';
  227. X                file_ext = CALLOC((strlen(buff) + 1), char);
  228. X                strcpy(file_ext, buff);
  229. X        }
  230. X        else
  231. X                file_ext = CALLOC(1, char);
  232. X                
  233. X        if(!path_p) {
  234. X                path = CALLOC((strlen(curr_path) + 1), char);
  235. X                strcpy(path, curr_path);
  236. X        }
  237. X
  238. X        free((char *)curr_path);
  239. X        
  240. X        if((new = MALLOC(path_t)) == NULL) {
  241. X                error("Can't instantiate Path");
  242. X                return(NULL);
  243. X        }
  244. X
  245. X        else {
  246. X                new->fil_disk = file_disk;
  247. X                new->fil_path = path;
  248. X                new->fil_name = file_name;
  249. X                new->fil_ext = file_ext;
  250. X                return(new);
  251. X        }
  252. X        /* all done */
  253. X}
  254. X
  255. X
  256. X/****************************************************************
  257. X * General Error Handling Utility                               *
  258. X ***************************************************************/
  259. X
  260. Xvoid error(string)
  261. Xchar *string;
  262. X{
  263. X        printf("ERROR : %s\n", string);
  264. X        exit(1);
  265. X}
  266. X
  267. END_OF_getpath.c
  268. if test 8092 -ne `wc -c <getpath.c`; then
  269.     echo shar: \"getpath.c\" unpacked with wrong size!
  270. fi
  271. # end of overwriting check
  272. fi
  273. if test -f getpath.h -a "${1}" != "-c" ; then 
  274.   echo shar: Will not over-write existing file \"getpath.h\"
  275. else
  276. echo shar: Extracting \"getpath.h\" \(2347 characters\)
  277. sed "s/^X//" >getpath.h <<'END_OF_getpath.h'
  278. X
  279. X/************************************************************************
  280. X *                                                                      *
  281. X * getpath.h:  Header File for getpath.c                                *
  282. X *                                                                      *
  283. X *                 Version 1.00  ---   May 19, 1988                     *
  284. X *                                                                      *
  285. X *              Copyright (c) 1988 by David O. Tinker                   *
  286. X *            All Rights Reserved except as noted below.                *
  287. X *                                                                      *
  288. X * This software may be used without charge for non-commercial purposes *
  289. X * provided this Copyright notice is not removed. Permission is granted *
  290. X * to make alterations to this software, provided credit is given to    *
  291. X * the original author in any altered version.                          *
  292. X *                                                                      *
  293. X *                      David O. Tinker                                 *
  294. X *                      Department of Biochemistry                      *
  295. X *                      University of Toronto                           *
  296. X *                      Toronto, Canada, M5S 1A8                        *
  297. X *                      E-mail:                                         *
  298. X *                      dtinker@utgpu.UUCP                              *
  299. X *                      dtinker@utoronto.BITNET                         *
  300. X *                                                                      *
  301. X ***********************************************************************/
  302. X
  303. X#include <stdio.h>
  304. X
  305. X#define BOOLEAN int
  306. X#define TRUE  1
  307. X#define FALSE 0
  308. X
  309. Xchar *malloc(), *calloc();
  310. X#define MALLOC(x)      ((x *) malloc(sizeof(x)))
  311. X#define CALLOC(n, x)   ((x *) calloc(n, sizeof(x)))
  312. X
  313. Xchar *getcwd(), *strcat();
  314. X
  315. X
  316. X/****************************************
  317. X * File Path Descriptor                 *
  318. X ***************************************/
  319. X /* structure to hold path data for current file */
  320. X
  321. Xstruct path_primitive
  322. X        {
  323. X                char *fil_disk;
  324. X                char *fil_path;
  325. X                char *fil_name;
  326. X                char *fil_ext;
  327. X        };
  328. X
  329. Xtypedef struct  path_primitive  path_t;
  330. Xpath_t *getpath();
  331. X
  332. END_OF_getpath.h
  333. if test 2347 -ne `wc -c <getpath.h`; then
  334.     echo shar: \"getpath.h\" unpacked with wrong size!
  335. fi
  336. # end of overwriting check
  337. fi
  338. if test -f sample.c -a "${1}" != "-c" ; then 
  339.   echo shar: Will not over-write existing file \"sample.c\"
  340. else
  341. echo shar: Extracting \"sample.c\" \(502 characters\)
  342. sed "s/^X//" >sample.c <<'END_OF_sample.c'
  343. X/* sample.c  -- example of use of getpath.c */
  344. X
  345. X#include "getpath.h"
  346. X
  347. Xmain()
  348. X{
  349. X     path_t *file_path;
  350. X     char   file_name[81];
  351. X
  352. X     printf("\n Enter a DOS file name: [d:][\path]name[.ext]\n");
  353. X     scanf("%s", file_name);
  354. X     file_path = getpath(file_name);
  355. X     printf("\n file disk = %s\n", file_path->fil_disk);
  356. X     printf(" file path = %s\n", file_path->fil_path);
  357. X     printf(" file name = %s\n", file_path->fil_name);
  358. X     printf(" file ext  = %s\n", file_path->fil_ext);
  359. X
  360. X     exit(0);
  361. X}
  362. X
  363. END_OF_sample.c
  364. if test 502 -ne `wc -c <sample.c`; then
  365.     echo shar: \"sample.c\" unpacked with wrong size!
  366. fi
  367. # end of overwriting check
  368. fi
  369. echo shar: End of shell archive.
  370. exit 0
  371. -----------------[ Cut Here ]--------------------
  372.  
  373. ---------------------------------------------------------------------------
  374. ! David O. Tinker             !     ^ ^     !      UUCP: utgpu!dtinker    !
  375. ! Department of Biochemistry  !   < O O >   !    BITNET: dtinker@utoronto !
  376. ! University of Toronto       !      ^      !       BIX: dtinker          !
  377. ! TORONTO, Ontario, Canada    !    #####    !     Voice: (416) 978-3636   !
  378. ! M5S 1A8                     !             ! And so on.                  !
  379. !                             !    Hi ho    !                             !
  380. ---------------------------------------------------------------------------
  381.